from django.conf import settings
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponseRedirect as redirect
from django.contrib.auth import authenticate
from StartGroup.models import Group
from groupInfo.forms import GroupInfoForm
import StartGroup.Constants
import Constants
import connectpeople.Constants
from groupInfo.models import Info
from  django.http import QueryDict
from utils import activity_module
from utils import common_query

# render form for Group Information/ Show group Info
def groupInfoFormView(request, group_web_address):
    group_web_addr = group_web_address
    group_info = getGroupInfo(group_web_addr)        
    
    # Get info row if exists
    try:
        information = getInfo(group_info)
    except Info.DoesNotExist:
        information = None        
    
    # Obtain user id
    userid = request.session[connectpeople.Constants.session_userid]
    
    # Get User object with permissions assigned
    permission = common_query.get_user_group_permission(userid, group_web_addr, False)
    
    if information == None:
        # If information does not already exists then render form
        information = ''
        # Group Info form initialization
        group_info_form = GroupInfoForm()
        return render_to_response("groupInfo/groupInfoForm.html",
                          {Constants.group_info_form_constant: group_info_form,'group':group_info,
                           'permission':permission},
                          context_instance = RequestContext(request))
    else:
        # Else take user to group info page
        return render_to_response("groupInfo/groupInfoPage.html",
                          {'information': information,'group':group_info,'permission':permission},
                          context_instance = RequestContext(request))
    

# Save the form
def info(request, group_web_address):
    
    group_web_addr = group_web_address
    group = getGroupInfo(group_web_addr)
    userid = request.session[connectpeople.Constants.session_userid]
    
    # Get info values if exist
    try:
        information = getInfo(group)
    except Info.DoesNotExist:
        information = None
        
    if request.method == 'POST':
        # If info values are updated then update the same row in table
        # (Do not create new row)
        if information != None:                
            information.info_head = request.POST.__getitem__('info_head')
            information.info = request.POST.__getitem__('info')
            information.contact_info = request.POST.__getitem__('contact_info')
            information.locations_info = request.POST.__getitem__('locations_info')
            information.save()
            
            # Group Info Update Activity save
            activity_module.setActivityNode(information.info_head, userid, group_web_addr, connectpeople.Constants.group_info_action)                
        else:
            # Save and insert new row in table
            # Group Info form initialization
            group_info_form = GroupInfoForm(data=request.POST)            
            if group_info_form.is_valid():
                group_info_form.save(group)
                
                # Group Info Update Activity save
                activity_module.setActivityNode(request.POST.__getitem__('info_head'), userid, group_web_addr, connectpeople.Constants.group_info_action)                    
    
    # Get User object with permissions assigned
    permission = common_query.get_user_group_permission(userid, group_web_addr, False)
                    
    return render_to_response("groupInfo/groupInfoPage.html",
                              {'information': information,'group':group, 'permission':permission},
                              context_instance = RequestContext(request))

# Edit group info form
def editGroupInfo(request, group_web_address):
    group_web_addr = group_web_address
    group_info = getGroupInfo(group_web_addr)
    
    # Get info values to show defaults in form
    information = getInfo(group_info)
    # Group Info form initialization
    group_info_form = GroupInfoForm()
    
    userid = request.session[connectpeople.Constants.session_userid]    
    
    # Get User object with permissions assigned
    permission = common_query.get_user_group_permission(userid, group_web_addr, False)  
    
    return render_to_response("groupInfo/groupInfoForm.html",
                              {'group_info_form': group_info_form, 'information': information,
                               'group':group_info,'permission':permission},
                              context_instance = RequestContext(request))



# Helper function to obtain Group related to a group_web_address
def getGroupInfo(group_web_addr):
    return Group.objects.get(group_web_address= group_web_addr)
    
def getInfo(group_info):
    return Info.objects.get(group=group_info)